import random  
import time  
from collections import deque  

# 模拟的检测函数  
def detection():  
    return random.choice(['none', 'tissue', 'mint', 'cake', 'candy'])  

# 消抖函数  
def debounce_detection(detection_func, debounce_time=3, window_size=5):  
    # 用于存储识别结果的队列  
    results_queue = deque(maxlen=window_size)  
    
    while True:  
        result = detection_func()  
        results_queue.append(result)  

        # 统计各个结果的频率  
        frequency = {}  
        for res in results_queue:  
            frequency[res] = frequency.get(res, 0) + 1  
        
        # 找出频率最高的结果  
        most_common_result = max(frequency, key=frequency.get)  

        # 只在结果改变时输出  
        if results_queue.count(most_common_result) >= len(results_queue) // 2:  #阈值  
            print(f"识别到的结果: {most_common_result}")  
        
        time.sleep(debounce_time)  # 模拟延迟  

# 启动消抖检测  
debounce_detection(detection)  